from zope.interface import implements
import os, sys

APPLICATION_INSTALL_DIR = os.environ.get('SIMPLEWATCHER_INSTALL')
if not APPLICATION_INSTALL_DIR:
    raise EnvironmentError("must set environment variable SIMPLEWATCHER_INSTALL")
sys.path.append('%s/lib' % APPLICATION_INSTALL_DIR)

from watcher.exceptions import WatcherError
from watcher.interfaces import IWatchSchedule, IWatchScheduler, ISchedulerTime
from watcher import WatcherContainer


class SchedulerTime(object):
    """should have args with the same general fields as cron"""
    implements(ISchedulerTime)
    def __init__(self, config, parent=None):
        if self.parent is not None:
            self.parent.setChild(self)
        self.config = config
        self.minutes = self.config.minutes.xml_text
        self.hours = self.config.hours.xml_text
        self.daysOfMonth = self.config.daysOfMonth.xml_text
        self.months = self.config.months.xml_text
        self.daysOfWeek = self.config.daysOfWeek.xml_text

    
class WatchSchedule(WatcherContainer):
    """watch scheduler, each watch should have one.  start and stop
       do not actually have to be set
    """
    implements(IWatchSchedule)
    def __init__(self, config, parent=None):
        super(WatchSchedule, self).__init__(parent)
        self.config = config
        self.start_time = SchedulerTime(self.config.start_time, parent=self)
        self.stop_time = SchedulerTime(self.config.stop_time, parent=self)
        